//管理账号信息
const DEBUG = false;
const USERS_KEY = 'USERS_KEY';
const TOKEN_KEY = "TOKEN_KEY";
const WECHAT_TOKEN_KEY = 'WECHAT_TOKEN_KEY';
const STATE_KEY = 'STATE_KEY';
// const API_URL = 'http://localhost:55720/';
 // const API_URL = 'http://192.168.3.18:8088/wlxc';
const API_URL = 'http://192.168.3.162:8080/wlxc';
 
 
 const BASE_URL='http://192.168.3.162:8080/'
const getPath = function(path) {
	if (!path) {
		return "";
	}
	if (path.indexOf('http') >= 0) {
		return path;
	} else {
		return API_URL + path;
	}
}
const hasLogin = function() {
	return !(getUser() == null);

	// return !(JSON.stringify(getUser())=="{}");
}
const checkResult = function(res) {
	try {
	
			if (res.msg=="未授权") {
				uni.showModal({
					title: "系统提示",
					content: "账户未进行登录或已过期",
					showCancel: false,
					success: function(cf) {
						if (cf.confirm) {
							uni.reLaunch({
								url: '../loginbycode/loginbycode'
							})
						}
					}
				})
			}

		
	} catch {

	}

}
const checkLogin = function(sucess, error) {
	if (!hasLogin()) {
		//没有用户信息
		weChatlogin(function() {
			login(function() {
				//success
				if (sucess) {
					sucess();
				}
			}, function() {
				if (error) {
					error();
				}
			});

		})

	} else {
		if (sucess) {
			sucess();
		}


	}


}
const getAuthorization = function() {
	var user = getUser();
	console.log("user=>",user);
	if (user == null) {
		return "";
	} else {
		return "Bearer " + user.access_token;
	}
}

const get = function(url, data, success, error) {
	var allurl = '';
	if (url.indexOf('//') > 0) {
		allurl = url;

	} else {
		allurl = API_URL + url
	}
	uni.showLoading({
		title: "加载中...",
		mask: true

	})
	uni.request({
		url: allurl,
		data: data,
		method: "GET",
		header: {
			"Content-Type": "application/x-www-form-urlencoded",
			'authorization': getAuthorization(), //自定义请求头信息
		},
		success: (res) => {



			console.log("url:" + allurl + " result:" + JSON.stringify(res.data));
			checkResult(res);
			success(res.data);
		},
		fail: (res) => {
			console.log("url:" + allurl + " result:" + JSON.stringify(res));

			error(res.data);

		},
		complete: () => {

			uni.hideLoading();
		}
	});

}

const deleteJson = function(url, data, success, error) {
	var allurl = '';
	if (url.indexOf('//') > 0) {
		allurl = url;

	} else {
		allurl = API_URL + url
	}
	uni.showLoading({
		title: "加载中...",
		mask: true

	})
	uni.request({
		url: allurl,
		data: data,
		method: "DELETE",
		header: {
			"Content-Type": "application/json",
			'authorization': getAuthorization(), //自定义请求头信息
		},
		success: (res) => {
			console.log("url:" + url + " result:" + JSON.stringify(res.data));
			checkResult(res);
			success(res.data);
		},
		fail: (res) => {
			console.log("url:" + url + " result:" + JSON.stringify(res));

			error(res);

		},
		complete: () => {

			uni.hideLoading();
		}
	});
}

const putJson = function(url, data, success, error, closeShowLoading) {
	var allurl = '';
	if (url.indexOf('//') > 0) {
		allurl = url;

	} else {
		allurl = API_URL + url
	}
	if (!closeShowLoading) {
		uni.showLoading({
			title: "加载中...",
			mask: true

		})
	}
	uni.request({
		url: allurl,
		data: data,
		method: "PUT",
		header: {
			"Content-Type": "application/json",
			'authorization': getAuthorization(), //自定义请求头信息
		},
		success: (res) => {
			console.log("url:" + url + " result:" + JSON.stringify(res.data));
			checkResult(res);
			success(res.data);
		},
		fail: (res) => {
			console.log("url:" + url + " result:" + JSON.stringify(res));

			error(res);

		},
		complete: () => {
			if (!closeShowLoading) {

				uni.hideLoading();
			}
		}
	});
}
const postJson = function(url, data, success, error) {
	var allurl = '';
	if (url.indexOf('//') > 0) {
		allurl = url;

	} else {
		allurl = API_URL + url
	}
	uni.showLoading({
		title: "加载中...",
		mask: true

	})
	uni.request({
		url: allurl,
		data: data,
		method: "POST",
	    dataType: 'json',
		header: {
			"Accept":"*/*",
			"connection":"keep-alive",
			"Content-Type": "application/json",
			'authorization': getAuthorization(), //自定义请求头信息
		},
		success: (res) => {
			console.log("url:" + url + " result:" + JSON.stringify(res.data));
			checkResult(res);
			success(res.data);
		},
		fail: (res) => {
			console.log("url:" + url + " result:" + JSON.stringify(res));
			if(error){
			error(res);
			}
		},
		complete: () => {

			uni.hideLoading();
		}
	});
}
const post = function(url, data, success, error) {
	var allurl = '';
	if (url.indexOf('//') > 0) {
		allurl = url;

	} else {
		allurl = API_URL + url
	}
	uni.showLoading({
		title: "加载中...",
		mask: true

	})
	uni.request({
		url: allurl,
		data: data,
		dataType: 'json',
		
		method: "POST",
		header: {
			"Content-Type": "application/x-www-form-urlencoded",
			'authorization': getAuthorization(), //自定义请求头信息
		},
		success: (res) => {


			console.log("url:" + url + " result:" + JSON.stringify(res.data));
			checkResult(res);
			success(res.data);
		},
		fail: (res) => {
			console.log("url:" + url + " result:" + JSON.stringify(res));

			error(res);

		},
		complete: function() {
			uni.hideLoading();
		}
	});

}
const setConfig = function(key, val) {

	uni.setStorageSync(key, val);


}
const getConfig = function(key) {
	//	return 'test';
	let ret = '';
	ret = uni.getStorageSync(key);
	if (!ret) {
		ret = '';
	}
	return ret;

}

const setToken = function(tokenInfo) {
	if(tokenInfo==null){
		uni.setStorageSync(TOKEN_KEY, null);
		
	}else{
		uni.setStorageSync(TOKEN_KEY, JSON.stringify(tokenInfo));
		
	}

}

const getToken = function() {
	let ret = '';
	ret = uni.getStorageSync(TOKEN_KEY);
	if (!ret) {
		return null;
	}
	return JSON.parse(ret);

}


var getUrlParam = function(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}

function urlencode(str) {
	str = (str + '').toString();

	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
	replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

const setUser = function(userInfo) {

	uni.setStorageSync(USERS_KEY, JSON.stringify(userInfo));
}

const getUser = function() {
	if (DEBUG) {
		return {
			uid: "0cba523a-cd60-403f-965d-3a32dcdb4e42",
			name:"tjl",
			access_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjQ1MzgyIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo0NTM4MiIsIm5hbWUiOiJ0amwiLCJsbiI6InRqbCIsInVpZCI6IjBjYmE1MjNhLWNkNjAtNDAzZi05NjVkLTNhMzJkY2RiNGU0MiIsImx0IjoiMjAyMy8xMS8yIDk6MzM6MTkiLCJuYmYiOjE2OTg4ODg3OTksImV4cCI6MTcwMTQ4MDc5OSwiaWF0IjoxNjk4ODg4Nzk5fQ.CyOhpLmgEzrDFfoSV5szv8wlR-lWgaLwkrU86Mk_jEs"
		}
	}
	let ret = '';
	ret = uni.getStorageSync(USERS_KEY);
	if (!ret) {
		return null;
	}
	return JSON.parse(ret);
}
export default {
	API_URL,
	BASE_URL,
	getPath,
	hasLogin,
	checkLogin,


	setToken,
	getToken,
	setConfig,
	getConfig,
	post,
	get,
	deleteJson,
	postJson,
	putJson,
	getAuthorization,
	setUser,
	DEBUG
}
